﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Edit : System.Web.UI.Page
{
    String strCon = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
    String strCom;
    SqlConnection objCon;
    SqlCommand objCom;
    SqlDataReader dtReader;
    protected void Page_Load(object sender, EventArgs e)
    {
        BindData(Convert.ToInt32(Request.QueryString["id"]));
        if (hidOwnerID.Value.ToString() != Session["strUserID"].ToString())
            btnDelete.Visible = false;
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        Label lblWDDescription = (Label)(e.Row.FindControl("lblWDDescription"));
        if (lblWDDescription != null)
            lblWDDescription.Text = DataBinder.Eval(e.Row.DataItem, "WDDescription").ToString();

        Label lblUsername = (Label)(e.Row.FindControl("lblUsername"));
        if (lblUsername != null)
            lblUsername.Text = DataBinder.Eval(e.Row.DataItem, "Username").ToString();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        if (txtWDDescription.Text == "")
            lblStatus.Text = "กรอกข้อมูล";
        else
            InsertData(Convert.ToInt32(Request.QueryString["id"]));
    }
    protected void btnDelete_Click(object sender, EventArgs e)
    {
        DeleteData(Convert.ToInt32(Request.QueryString["id"]));
    }
    protected void BindData(int id)
    {
        //Bind
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "Select WOwner,WName,WDescription,Username From Webboards Inner Join Users On UserID = WOwner Where WID = '" + id + "'";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();

        while (dtReader.Read())
        {
            hidOwnerID.Value = dtReader[0].ToString();
            txtWName.Text = dtReader[1].ToString();
            txtWDescription.Text = dtReader[2].ToString();
            txtOwner.Text = dtReader[3].ToString();
        }

        dtReader.Close();
        dtReader = null;

        //Grid
        strCom = "Select T1.WDDescription,T2.Username from WebboardsDetail T1 Inner Join Users T2 On T1.WOwnerID = T2.UserID Where T1.WID = '" + id + "' Order by WDID";
        objCom = new SqlCommand(strCom, objCon);

        dtReader = objCom.ExecuteReader();
        GridView1.DataSource = dtReader;
        GridView1.DataBind();

        dtReader.Close();
        dtReader = null;
    }
    protected void InsertData(int id)
    {
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "Insert Into WebboardsDetail (WID,WOwnerID,WDDescription) " +
            " VALUES "
            + " ('" + id
            + "','" + Session["strUserID"]
            + "','" + txtWDDescription.Text + "')";
        objCom = new SqlCommand(strCom, objCon);
        try
        {
            objCom.ExecuteNonQuery();
            Response.Redirect(Request.RawUrl);
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    protected void DeleteData(int id)
    {
        objCon = new SqlConnection(strCon);
        objCon.Open();
        strCom = "Delete From Webboards Where WID = " + id +
            ";Delete From WebboardsDetail Where WID = " + id;
        objCom = new SqlCommand(strCom, objCon);
        objCom.ExecuteNonQuery();

        Response.Redirect("Home.aspx");
    }
}